package com.ruiyian.project.equipment.controller;

import com.google.common.collect.Maps;
import com.ruiyian.common.utils.poi.ExcelUtil;
import com.ruiyian.common.vo.TreeSelect;
import com.ruiyian.common.core.domain.entity.Equipment;
import com.ruiyian.project.equipment.service.EquipmentService;
import com.ruiyian.project.road.domain.DirePoint;
import com.ruiyian.project.road.service.DireService;
import com.ruiyian.project.tunnel.domain.Hole;
import com.ruiyian.project.tunnel.service.TunnelService;
import com.ruiyian.common.core.controller.BaseController;
import com.ruiyian.common.core.domain.AjaxResult;
import com.ruiyian.common.core.page.TableDataInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author chong
 * @email 673426164@qq.com
 * @create 2021/1/19
 * Desc: 设备控制层
 */
@RestController
@RequestMapping("/equipment/equipmentInfo")
public class EquipmentController extends BaseController {
    @Autowired
    private EquipmentService equipmentService;
    @Autowired
    private TunnelService tunnelService;
    @Autowired
    private DireService direService;

    /**
     * 设备类表
     * @param equipment
     * @return
     */
    @PreAuthorize("@ss.hasPermi('equipment:eq:list')")
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody Equipment equipment) {
        startPage();
        List<Equipment> list = equipmentService.selectEquiList(equipment);
        return getDataTable(list);
    }

    /**
     * 查询所有
     * @param equipment
     * @return
     */
    @PostMapping("/selectAll")
    public AjaxResult delete(@RequestBody Equipment equipment) {
        List<Equipment> list = null;
        try {
            list = equipmentService.selectEquiList(equipment);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success(list);
    }

    /**
     * 获取虚拟设备 转经纬度
     * @param equipment
     * @return
     */
    @PostMapping("/queryListIfVirtual")
    public AjaxResult queryListIfVirtual(@RequestBody Equipment equipment) {
        Map<String, Object> data = Maps.newHashMap();
        List<Equipment> list = null;
        try {
            list = equipmentService.selectEquiList(equipment);
            data.put("list",list);
            List<DirePoint> direPoints = direService.queryByRoadId(equipment.getRoadId());
            Map<Long, List<DirePoint>> collect = direPoints.stream().collect(Collectors.groupingBy(DirePoint::getRoadDireId));
            data.put("direPoint",collect);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success(data);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @PreAuthorize("@ss.hasPermi('equipment:eq:remove')")
    @PostMapping("/delete")
    public AjaxResult delete(@RequestBody Long id) {
        try {
            equipmentService.delete(id);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success();
    }

    /**
     * 批量删除
     * @param list
     * @return
     */
    @PreAuthorize("@ss.hasPermi('equipment:eq:remove')")
    @PostMapping("/deleteBatch")
    public AjaxResult deleteBatch(@RequestBody List<Equipment> list) {
        try {
            equipmentService.deleteBatch(list);
        } catch (Exception e) {
            logger.error(e.getMessage());
            AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success();
    }

    /**
     * 根据id获取对象
     * @param id
     * @return
     */

    @PreAuthorize("@ss.hasPermi('equipment:eq:edit')")
    @PostMapping("/findOne")
    public AjaxResult findOne(@RequestBody Long id) {
        Equipment equipment = null;
        try {
            equipment =  equipmentService.findOne(id);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success(equipment);
    }

    /**
     * 保存
     * @param equipment
     * @return
     */
    @PreAuthorize("@ss.hasPermi('equipment:eq:edit')")
    @PostMapping("/save")
    public AjaxResult save(@RequestBody Equipment equipment) {
        try {
            if(equipment.getId()!=null){
                equipmentService.edit(equipment);
            }else {
                equipmentService.save(equipment);
            }
        } catch (Exception e) {
            logger.error(e.getMessage());
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success(equipment);
    }

    /**
     * 导出
     * @param equipment
     * @return
     */
    @PreAuthorize("@ss.hasPermi('equipment:eq:export')")
    @GetMapping("/export")
    public AjaxResult export(Equipment equipment) {
        List<Equipment> list = equipmentService.selectEquiList(equipment);
        ExcelUtil<Equipment> util = new ExcelUtil<>(Equipment.class);
        return util.exportExcel(list, "设备信息");
    }

    /**
     * 导入
     * @param file
     * @return
     * @throws Exception
     */
    @PreAuthorize("@ss.hasPermi('equipment:eq:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception {
        List<Equipment> equipmentList = null;
        try {
            ExcelUtil<Equipment> util = new ExcelUtil<>(Equipment.class);
            equipmentList = util.importExcel(file.getInputStream());
            equipmentService.importData(equipmentList);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return AjaxResult.error("导入失败");
        }
        return AjaxResult.success(equipmentList);
    }

    /**
     * 根据隧道id和模块获取隧道设备
     * @param equipment
     * @return
     */
    @PostMapping("/selectEquiListByTunnelId")
    public AjaxResult selectEquiListByTunnelId(@RequestBody Equipment equipment) {
        List<Hole> list = null;
        try {
            list = tunnelService.selectHoleById(equipment.getTunnelId());
            for(Hole h: list){
                List<Equipment> equipmentList = equipmentService.selectEquiListByTunnelId(new Equipment(equipment.getTunnelId(), h.getDirectionId(),equipment.getModuleId()));
                h.setEquipmentList(equipmentList);
            }
        } catch (Exception e) {
            logger.error(e.getMessage());
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success(list);
    }

    /**
     * 根据设备类型查询道路设备
     * @param equipment
     * @return
     */
    @PostMapping("/treeEq")
    public AjaxResult treeEq(@RequestBody Equipment equipment) {
        List<TreeSelect> treeSelects = null;
        try {
            treeSelects = equipmentService.treeEqNoPoint(equipment);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success(treeSelects);
    }

    /**
     * 修改编号 code
     * @param equipment
     */
    @PostMapping("/updateCode")
    public AjaxResult updateCode(@RequestBody Equipment equipment) {
        List<Equipment> list = null;
        try {
            equipmentService.updateCode(equipment);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success(list);
    }

    /**
     * 修改点位
     * @param list
     * @return
     */
    @PostMapping("/updatePoint")
    public AjaxResult updatePoint(@RequestBody List<Map<String,String>> list) {
        try {
            for(Map<String,String> m: list){
                Equipment equipment = new Equipment();
                equipment.setId(Long.parseLong(m.get("id")));
                equipment.setLongitude(m.get("lng"));
                equipment.setLatitude(m.get("lat"));
                equipmentService.updatePoint(equipment);
            }
        } catch (Exception e) {
            logger.error(e.getMessage());
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success(list);
    }

    /**
     * 导出设备模板
     * @return
     */
    @GetMapping("/importTemplate")
    public AjaxResult importTemplate()
    {
        ExcelUtil<Equipment> util = new ExcelUtil<>(Equipment.class);
        return util.importTemplateExcel("" +
                "设备模板");
    }

    /**
     * 获取监控设备
     * @param tunnelId
     * @return
     */
    @PostMapping("/selectVideoList")
    public AjaxResult selectVideoList(@RequestBody Long tunnelId) {
        List<Equipment> list = null;
        try {
            list = equipmentService.selectVideoList(tunnelId);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success(list);
    }
}
